package com.iscc.maoke.dao.impl;

import com.iscc.maoke.dao.ImgDAO;
import com.iscc.maoke.domain.Img;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

public class ImgDAOImpl implements ImgDAO {
    @Override
    public int add(Connection conn, List<Img> imgs) {
        System.out.println("imgs = " + imgs);

        int result = -1;
        int count = 0;
        String sql = "INSERT INTO tab_img(ipath,pno) VALUES ";
        for (Img img : imgs) {
            sql += " (?,?) ,";
            count += 2;
        }
        sql = sql.substring(0, sql.lastIndexOf(","));

        try {
            PreparedStatement psmt = conn.prepareStatement(sql);

            for (int i = 1, j = 0; i <= count; i += 2, j++) {
                Img img = imgs.get(j);
                psmt.setString(i, img.getIpath());
                psmt.setInt(i + 1, img.getPno());

            }
            result = psmt.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }


        return result;
    }

    @Override
    public int add(Object o) {
        return 0;
    }

    @Override
    public int update(Object o) {
        return 0;
    }

    @Override
    public int del(int id) {
        return 0;
    }

    @Override
    public List queryAll() {
        return null;
    }

    @Override
    public Object queryById(int id) {
        return null;
    }
}
